﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ADC.DAL;

namespace ADC.BL
{

    public class ProductTypeItem
    {
        public int TypeId { get; set; }
        public string Title { get; set; }
        public String Description { get; set; }
        public DateTime ModifiedDate { get; set; }
        public String ImagePath { get; set; }
    }

    public class ProductTypeFactory
    {
        public static List<ProductType> SelectAll()
        {
            var db = new dbADCEntities();
            return db.ProductTypes.OrderBy(g => g.Sort).ToList();
        }

        public static ProductType GetByID(int typeID)
        {
            var db = new dbADCEntities();
            return db.ProductTypes.Where(g => g.ProductTypeID.Equals(typeID)).FirstOrDefault();
        }


        public static ProductType Insert(ProductType productType)
        {
            var db = new dbADCEntities();
            productType.ModifiedDate = DateTime.Now;
            db.AddToProductTypes(productType);
            db.SaveChanges();
            return productType;
        }

        public static int Update(ProductType productType)
        {
            try
            {
                var db = new dbADCEntities();
                var objPrograme = new ProductType();
                objPrograme = db.ProductTypes.Where(g => g.ProductTypeID == productType.ProductTypeID).FirstOrDefault();
                objPrograme.Title = productType.Title;
                objPrograme.Description = productType.Description;
                objPrograme.Sort = productType.Sort;
                objPrograme.ModifiedDate = DateTime.Now;
                return db.SaveChanges();
            }
            catch (Exception)
            {
                return -1;
                // Log here
                throw;
            }
        }


        public static int delete(int typeID)
        {
            try
            {
                var db = new dbADCEntities();
                var objPrograme = new ProductType();
                //delete reference programe
                List<Product> products = db.Products.Where(g => g.ProductType == typeID).ToList();
                foreach (Product pr in products)
                {
                    ProductFactory.Delete(pr.ProductID);
                }

                
                objPrograme = db.ProductTypes.Where(g => g.ProductTypeID == typeID).FirstOrDefault();
                db.DeleteObject(objPrograme);
                return db.SaveChanges();
            }
            catch (Exception)
            {
                return -1;
                // Log here
                throw;
            }
        }

    }
}
